package com.sun.weekend.view;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.sun.weekend.R;
import com.sun.weekend.util.LogUtils;

/**
 * @author sunleilei
 */

public class VideoPlayer extends FrameLayout implements View.OnClickListener {


    Button fullscreen;
    ImageView imageview;
    RelativeLayout frame;
    GestureDetector detector;
    private VideoPlayerListener ls;

    public VideoPlayer(Context context) {
        this(context,null);
    }

    public VideoPlayer(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public VideoPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.player_layout,null);
        imageview = (ImageView) view.findViewById(R.id.imageview);
        frame = (RelativeLayout) view.findViewById(R.id.frame);
        fullscreen = (Button) view.findViewById(R.id.fullscreen);
        fullscreen.setOnClickListener(this);
        detector = new GestureDetector(getContext(),new GestureListener());
        addView(view);
    }

    @Override
    public void onClick(View v) {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            ((Activity)getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }else{
            ((Activity)getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }


    public void setOnPlayerListener(VideoPlayerListener ls) {
        this.ls = ls;
    }

    public interface VideoPlayerListener{
        void postion(int pos);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogUtils.logi("onTouchEvent");
        return true;
    }

    public class GestureListener extends GestureDetector.SimpleOnGestureListener{
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }
}
